#include "stdio.h"
#include "unistd.h"
#include "sys/types.h"
#include "sys/stat.h"
#include "fcntl.h"
#include "stdlib.h"
#include "string.h"
/*************************************************
Copyright @ tiny
file        :ledApp.c
author      :Tiny
version     :1.0.0
description :led driver test
other       :none
usage       : ./ledApp /dev/led 0 turn off led
              ./ledApp /dev/led 1 turn on led
log         :first version 2023-03-02
*************************************************/
#define     LEDOFF      0
#define     LED_ON      1

int main(int argc, char *argv[])
{
    int fd, retvalue;
    char *filename;
    unsigned char databuf[1];

    if(argc != 3){
        printf("Error Usage!\n");
        return -1;
    }
    filename = argv[1];
    
    /* open led device file */
    fd = open(filename, O_RDWR);
    if(fd < 0){
        printf("open led device file(%s) failed!\n", argv[1]);
        return -1;
    }

    databuf[0] = atoi(argv[2]);  /* get operation value,0:off,1:on */

    /* write operation value to led device file */
    retvalue = write(fd, databuf, sizeof(databuf));
    if(retvalue < 0){
        printf("write operation value to led device file failed!\n");
        close(fd);
        return -1;
    }

    retvalue = close(fd); /* close led device file */
    if(retvalue < 0){
        printf("close led device file(%s) failed.\n", argv[1]);
        return -1;
    }
    return 0;
}